<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Award;

class AwardController extends BaseController
{

    //
    public function list(Request $request)
    {
        $data = Award::query()->get()->toArray();

        return $this->success($data);
    }

    public function edit(int $id, Request $request)
    {
        $request->validate([
            'name' => 'required|max:10|min:2',
            'inventory' => 'required|numeric',
            'money' => 'required|numeric',
        ]);

        $post = $request->post();

        $award = Award::query()->findOrNew($id);

        $award->name = $post['name'];
        $award->inventory = $post['inventory'];
        $award->money = $post['money'];

        $award->save();

        return $this->success($award);
    }

    public function detail(int $id)
    {
        $data = Award::query()->find($id)->toArray();

        return $this->success($data);
    }

    public function search(Request $request)
    {
        $name = $request->query('name', '');

        $query = Award::query();

        if ($name) {
            $query = $query->where('name', $name);
        }

        $data = $query->get(['id', 'name'])->toArray();

        return $this->success($data);
    }
}
